<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Post extends CI_Controller {

    private $limit = 3;
    private $post_type = 'post';

    function __construct() {
        parent::__construct();
        $this->load->library(array('table', 'form_validation'));
        $this->load->model('Post_model', '', TRUE);
        $this->load->helper('url');

    }

    function index($offset = 0, $post_type = '') {
        $data['title'] = 'Post page';
        
        // offset
        $uri_segment = 3;
        $offset = $this->uri->segment($uri_segment);
        //post_type
        if(strlen($post_type) != 0){
            $this->post_type = $post_type;
        }
        
        // load data
        $posts = $this->Post_model->get_paged_list($this->limit, $offset)->result();
        // generate pagination
        $this->load->library('pagination');
        
        $config = array();
        $config['base_url'] = site_url('post/index/');
        $config['total_rows'] = $this->Post_model->count_all('post');
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        
        //merge data to array
        $data['pagination'] = $this->pagination->create_links();
        $data['template_type'] = 'content';
        $data['data'] = $posts;
        
        //send to view
        $this->load->view('template',$data);
    }

    function view($id) {
        // get post details
        
        $data['template_type'] = 'single';
        $post = $this->Post_model->get_by_id($id)->row();
        $data['data'] = $post;
        $data['title'] = $post->post_title;
        
        $this->load->view('template', $data);
    }
    
    function add() {
        $this->_set_fields();
        $this->_set_rules();

        // set common properties
        $data['title'] = 'Add new post';
        $data['message'] = 'Add new post';
        $data['action'] = site_url('post/addpost');
        $data['link_back'] = anchor('post/index/', 'Back to list of posts', array('class' => 'back'));
        
        $data['template_type'] = 'post_edit';
        $data['data'] = '';
        $this->load->view('template', $data);
    }

    function addpost() {
        $data = array();
        
        $data['title'] = 'Add new post';
        $data['action'] = site_url('post/addpost');
        $data['link_back'] = anchor('post/index/', 'Back to list of posts', array('class' => 'back'));

//        $this->_set_fields();
//        $this->_set_rules();

            // save data
            $post = array('post_title' => $this->input->post('title'),
                'post_content' => $this->input->post('content')
                );
            $id = $this->Post_model->save($post);

            // set user message
            $data['message'] = '<div class="success">add new post success</div>';
            
        $data['template_type'] = 'post_edit';
        $data['data'] = '';
        $this->load->view('template', $data);
    }

    function update($id) {
        // set validation properties
        $this->_set_rules();

        // prefill form values
        $post = $this->Post_model->get_by_id($id)->row();
        $this->form_data->id = $id;
        $this->form_data->name = $post->name;
        $this->form_data->gender = strtoupper($post->gender);
        $this->form_data->dob = date('d-m-Y', strtotime($post->dob));

        // set common properties
        $data['title'] = 'Update post';
        $data['message'] = '';
        $data['action'] = site_url('post/updatepost');
        $data['link_back'] = anchor('post/index/', 'Back to list of posts', array('class' => 'back'));

        // load view
        $this->load->view('postEdit', $data);
    }

    function updatepost() {
        // set common properties
        $data['title'] = 'Update post';
        $data['action'] = site_url('post/updatepost');
        $data['link_back'] = anchor('post/index/', 'Back to list of posts', array('class' => 'back'));

        $this->_set_fields();
        $this->_set_rules();

        // run validation
        if ($this->form_validation->run() == FALSE) {
            $data['message'] = '';
        } else {
            // save data
            $id = $this->input->post('id');
            $post = array('name' => $this->input->post('name'),
                'gender' => $this->input->post('gender'),
                'dob' => date('Y-m-d', strtotime($this->input->post('dob'))));
            $this->Post_model->update($id, $post);

            $data['message'] = '<div class="success">update post success</div>';
        }

        $this->load->view('postEdit', $data);
    }

    function delete($id) {
        $this->Post_model->delete($id);

        redirect('post/index/', 'refresh');
    }

    function test(){
        $this->router->show_404();
    }
    
    
    // set empty default form field values
    function _set_fields() {
        $this->form_data->id = '';
        $this->form_data->name = '';
        $this->form_data->gender = '';
        $this->form_data->dob = '';
    }

    // validation rules
    function _set_rules() {
        $this->form_validation->set_rules('name', 'Name', 'trim|required');
        $this->form_validation->set_rules('gender', 'Gender', 'trim|required');
        $this->form_validation->set_rules('dob', 'DoB', 'trim|required|callback_valid_date');

        $this->form_validation->set_message('required', '* required');
        $this->form_validation->set_message('isset', '* required');
        $this->form_validation->set_message('valid_date', 'date format is not valid. dd-mm-yyyy');
        $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
    }

    // date_validation callback
    function valid_date($str) {
        //match the format of the date
        if (preg_match("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $str, $parts)) {
            //check weather the date is valid of not
            if (checkdate($parts[2], $parts[1], $parts[3]))
                return true;
            else
                return false;
        }
        else
            return false;
    }

}

?>